package model;

import exception.BuildNameNotSetException;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.ini4j.Wini;

public class ConfigManager {

    private static Wini cfg;
    private static final String FILENAME = "C:\\Documents and Settings\\dongxu\\My Documents\\NetBeansProjects\\test\\cfg\\default.cfg";
    private static final String FILENAME_BACK1 = "C:\\Users\\think\\Documents\\NetBeansProjects\\test\\cfg\\default.cfg";


    static {
        File[] configFiles = new File[]{new File(FILENAME), new File(FILENAME_BACK1)};
        cfg = ConfigLoader.loadConfig(configFiles);

//        try {
//            cfg = new Wini(new File(FILENAME));
//        } catch (IOException ex) {
//            try {
//                cfg = new Wini(new File(FILENAME_BACK1));
//            } catch (IOException ex1) {
//                Logger.getLogger(ConfigManager.class.getName()).log(Level.SEVERE, null, ex1);
//            }
//        }
    }
    private static String build;
    private static String machine;

    public static boolean reload() {
        cfg = null;
        cfg = ConfigLoader.loadConfig(new File[]{new File(FILENAME), new File(FILENAME_BACK1)});
        return cfg == null ? false : true;
    }

    public static int getNewBuildWatchFrequency() {
        String frequency = cfg.get("server", "new_build_watch_frequency").trim();
        return new Integer(frequency).intValue();
    }

    public static String getCurrentInstallerArchive() throws BuildNameNotSetException {
        return getInstallerPath() + "\\" + getBuildName() + ".zip";
    }

    public static String getUsageFilePath() {
        return cfg.get("server", "usage_file");
    }

    public static int getShowCmdHistoryCount() {
        String count = cfg.get("server", "show_command_history").trim();
        return new Integer(count).intValue();
    }

    public static int getShowTestResultHistoryCount() {
        String count = cfg.get("server", "show_testresult_history").trim();
        return new Integer(count).intValue();
    }

    static String getBiuldReleasePath() {
        return cfg.get("server", "build_release_path");
    }

    static void setMachine(String m) {
        machine = m;
    }

    static String getMachine(){
        return machine;
    }

    private static String getCfgPath() throws IOException {
        File pwd = new File(".");
        return pwd.getCanonicalPath() + "\\cfg\\";
    }

    public static String getBuildName() throws BuildNameNotSetException {
        if (build == null) {
            throw new BuildNameNotSetException();
        }
        return build;
    }

//    public static boolean reload() {
//        try {
//            cfg = new Wini(new File(getCfgPath()+"default.cfg"));
//            return true;
//        } catch (IOException ex) {
//            Logger.getLogger(VMMachineFactory.class.getName()).log(Level.SEVERE, null, ex);
//            return false;
//        }
//    }
    public static String getLogPath() {
        return cfg.get("server", "log_path");
    }

    public static String getProjectPath() {
        return cfg.get("server", "project_path");
    }

    public static String getServerHostName() {
        return cfg.get("server", "hostname");
    }

    public static String getInstallerPath() {
//        return installerPath;
        return cfg.get("server", "installer_path");
    }

    public static String getVMMachineCfgFile() {
        return cfg.get("server", "vm_cfg_file");
//        return "C:\\Documents and Settings\\dongxu\\My Documents\\NetBeansProjects\\test\\cfg\\machines.cfg";
    }

    public static String isStartedHandleName() {
        return cfg.get("handleName", "is_vm_started");
    }

    public static String getSnapshotsHandleName() {
        return cfg.get("handleName", "get_snapshots");
    }

    public static String revertToSnapshotHandleName() {
        return cfg.get("handleName", "revert_vm");
    }

    public static void setBuildName(String buildName) {
        build = buildName;
    }

    public static String startVMHandleName() {
        return cfg.get("handleName", "start_vm");
    }

    public static String runTSHandleName() {
        return cfg.get("handleName", "run_script");
    }

    public static String getProjectArchiveName() {
        return cfg.get("server", "project_archive_name");
    }

    public static String getZipHandleName() {
        return cfg.get("handleName", "zip");
    }

    public static String getFSHandleName() {
        return cfg.get("handleName", "copy_file");
    }

    public static String getUnzipHandleName() {
        return cfg.get("handleName", "unzip");
    }

    public static String getTakeSnapshotHandleName() {
        return cfg.get("handleName", "take_snapshot");
    }

    public static String getStopVMHandleName() {
        return cfg.get("handleName", "stop_vm");
    }

    public static String getListHandlesHandleName() {
        return cfg.get("handleName", "list_handles");
    }

    public static void main(String[] args) {
        try {
            File pwd = new File(".");
            System.out.println(pwd.getCanonicalPath());
            System.out.println(ConfigManager.getLogPath());
            System.out.println(ConfigManager.getShowCmdHistoryCount());
            System.out.println(ConfigManager.getNewBuildWatchFrequency());
        } catch (IOException ex) {
            Logger.getLogger(ConfigManager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
